package com.atguigu.ucenterservice.service.impl;

import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.servicebase.handler.GuliException;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.LoginVo;
import com.atguigu.ucenterservice.entity.vo.RegisterVo;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.atguigu.ucenterservice.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
   @Autowired
   private RedisTemplate<String,String>redisTemplate;
    //用户注册
    @Override
    public void register(RegisterVo registerVo) {
        //1.取出参数、验空
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        String password = registerVo.getPassword();
        if(StringUtils.isEmpty(nickname)||StringUtils.isEmpty(mobile)||
         StringUtils.isEmpty(code)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册信息缺失");
        }
        //2.验证手机号是否重复
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        //统计查询的结果
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw new GuliException(20001,"该手机号已经注册");
        }
        //3.验证校验码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码有误或者已失效");
        }
        //4.MD5加密密码,生成密文
        String encryptPassword = MD5.encrypt(password);
        //5.补充信息后存入数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(encryptPassword);
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        ucenterMember.setIsDisabled(false);
        //存入到数据库
        baseMapper.insert(ucenterMember);
    }

    //用户登录
    @Override
    public String login(LoginVo loginVo) {
        //1.取出参数,进行验空
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"手机号或者密码有误");
        }
        //2.验证手机号是否存在，并取出信息
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember==null){
            throw new GuliException(20001,"手机号或者密码有误");
        }
        //3.验证密码，验证密文
        String encryptPassword = MD5.encrypt(password);
        if(!encryptPassword.equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"手机号或者密码有误");
        }
        //4.生成token字符串，返回
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }
//根据日期统计注册信息远程调用
    @Override
    public Integer countRegister(String day) {
        Integer count=baseMapper.countRegister(day);
        return count;
    }
}
